﻿using System.Collections.Generic;
using System.Data;
using Besture.Common;
using Besture.UI.Admin.Master;
using Besture.UI.ClassHelper;
using System.IO;


namespace Besture.UI.Admin.Content
{
    public partial class TemplateList : BasePage
    {
        //模板所在根目录
        public static string RootTemplate = string.Empty;

        /// <summary>
        /// 初始化页面
        /// </summary>
        protected override void InitialLoad()
        {
            if(!string.IsNullOrEmpty(StringHelper.Query("Path")))
            {
                RootTemplate = StringHelper.Query("Path");
            }
            else
            {
                RootTemplate = "~";
            }

            BindData();
            AspxGridViewHelper.InitAspxGridView(grid);

            ((ConfigAdmin)this.Master).Title = "文件管理";
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            BindData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            //过滤掉的文件类型
            List<string> listFilterFileType = new List<string>();
            listFilterFileType.Add(".cs");
            listFilterFileType.Add(".csproj");
            listFilterFileType.Add(".user");
            listFilterFileType.Add(".asax");
            listFilterFileType.Add(".config");
            

            //过滤掉的文件夹
            List<string> listFilterFolder = new List<string>();
            listFilterFolder.Add("admin");
            listFilterFolder.Add("app_code");
            listFilterFolder.Add("app_data");
            listFilterFolder.Add("app_globalresources");
            listFilterFolder.Add("bin");
            listFilterFolder.Add("Control");
            listFilterFolder.Add("obj");
            listFilterFolder.Add("properties");

            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof (string));
            table.Columns.Add("FileType", typeof (string));
            table.Columns.Add("Link", typeof (string));
            table.Columns.Add("Length", typeof (string));
            table.Columns.Add("UpdateTime", typeof (string));
            table.Columns.Add("Icon", typeof (string));
            

            string path = Server.MapPath(RootTemplate);
            
            //查询根目录下的文件
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            FileSystemInfo[] fileSystemInfo = directoryInfo.GetFileSystemInfos();
            //文件夹
            for (int i = 0; i < fileSystemInfo.Length; i++)
            {
                if(string.IsNullOrEmpty(fileSystemInfo[i].Extension))
                {
                    //过滤相关文件夹
                    if (listFilterFolder.Contains(fileSystemInfo[i].Name.ToLower()))
                    {
                        continue;
                    }
                    DataRow row = table.NewRow();
                    row["Name"] = fileSystemInfo[i].Name;
                    row["FileType"] = fileSystemInfo[i].Extension +  "文件";
                    row["Link"] = string.Format(@"?path={0}/{1}", RootTemplate, fileSystemInfo[i].Name);
                    row["UpdateTime"] = fileSystemInfo[i].LastWriteTime;
                    row["Icon"] = "folder.gif";
                    table.Rows.Add(row);
                }
                
            }
            //文件
            for (int i = 0; i < fileSystemInfo.Length; i++)
            {
                if(!string.IsNullOrEmpty(fileSystemInfo[i].Extension))
                {
                    //过滤相关文件类型
                    if (listFilterFileType.Contains(fileSystemInfo[i].Extension))
                    {
                        continue;
                    }
                    DataRow row = table.NewRow();
                    row["Name"] = fileSystemInfo[i].Name;
                    row["FileType"] = fileSystemInfo[i].Extension + "文件";
                    row["Link"] = string.Format(@"javascript:ShowTemplate('{0}/{1}')", RootTemplate, fileSystemInfo[i].Name);
                    row["UpdateTime"] = fileSystemInfo[i].LastWriteTime;
                    row["Icon"] = "html.gif";
                    table.Rows.Add(row);
                }
            }

            grid.DataSource = table;
            grid.DataBind();
        }

        
    }
}